import { useCopyToClipboard } from '@/hooks/web/useCopyToClipboard'
import { ref } from 'vue'

export default function useCopyValue() {
  const { clipboardRef, copiedRef } = useCopyToClipboard()

  const copyMessage = ref('')
  let timeId: number
  function closingSoon() {
    clearTimeout(timeId)
    timeId = (setTimeout as Window['setTimeout'])(() => {
      copyMessage.value = ''
    }, 1600)
  }

  function handleCopy(value: string) {
    // const value = copyValue.value
    if (!value) {
      copyMessage.value = '请输入要拷贝的内容！'
      closingSoon()
      return
    }
    clipboardRef.value = value
    if (copiedRef.value) {
      copyMessage.value = '复制成功'
      closingSoon()
    }
  }

  return { copyMessage, handleCopy }
}
